<?php
define('ERROR_LOAD_PNG','1');
define('ERROR_LOAD_IMAGE','2');
define('ERROR_SET_X','3');
define('ERROR_SET_Y','4');
define('ERROR_UNKNOWN_EXT','5');
define('ERROR_OPACITY','6');
define('ERROR_IMAGE_NOT_SET','7');
define('ERROR_WTMRKIMAGE_NOT_SET','8');
define('ERROR_IMAGE_X_NOT_SET','9');
define('ERROR_IMAGE_Y_NOT_SET','10');
define('ERROR_UNKNOWN_TYPE','11');
define('ERROR_FILE_NOT_FOUND','11');


#############
//define('DEBUG','1');

class watermark {
    public $wtmrkimage;
    public $srcimage;
    public $posX;
    public $posY;
    public $opacity;
    public $error;
    public $type;

    function __construct($image=false,$wtmrkimage=false,$pos=false,$opacity=100,$resize=1) {
	if ( defined('DEBUG') ) echo "Constructor start\n";
	$this->error=0;
	if ($image ) $this->SetImage($image);
	if ($wtmrkimage) $this->SetWtmrkImage($wtmrkimage,$resize);
	if ( $pos ) $this->SetPosition($pos['x'],$pos['y']);
	if ( $opacity) $this->SetOpacity($opacity);;
	if ( defined('DEBUG') ) echo "Constructor end\n";
    }

    function ShowError() {
	echo '[ X ] ';
	switch($this->error) {
	    case ERROR_LOAD_PNG: echo 'Can\'t load PNG image'; break;
	    case ERROR_LOAD_IMAGE: echo 'Failed to load main image'; break;
	    case ERROR_SET_X: echo 'Failed to set X coordinate'; break;
	    case ERROR_SET_Y: echo 'Failed to set Y coordinate'; break;
	    case ERROR_UNKNOWN_EXT: echo 'Unknown extention for image'; break;
	    case ERROR_OPACITY: echo 'Failed to set opacity'; break;
	    case ERROR_IMAGE_NOT_SET: echo 'Image resource not loaded'; break;
	    case ERROR_WTMRKIMAGE_NOT_SET: echo 'Watermark image resource not loaded'; break;
	    case ERROR_IMAGE_X_NOT_SET: echo 'Image x coordinate not loaded'; break;
	    case ERROR_IMAGE_Y_NOT_SET: echo 'Image y coordinate not loaded'; break;
	    case ERROR_UNKNOWN_TYPE: echo 'Type of image is unknown'; break;
	    case ERROR_FILE_NOT_FOUND: echo 'File not found'; break;
	    default: echo 'Unknown Error'; break;
	}
	echo "\n";
	return ;
    }

    function SetImage($image) {
	if ( defined('DEBUG') ) echo "SetImage start\n";
	if ( !is_file($image) ) { $error=ERROR_FILE_NOT_FOUND; return ; }
	preg_match('%.*\.([a-z]+)%si',$image,$ext);
	switch($ext[1]) {
	    case 'png': $this->type=$ext[1]; $this->srcimage=imagecreatefrompng($image); break;
	    case 'jpeg': $this->type=$ext[1]; $this->srcimage=imagecreatefromjpeg($image); break;
	    case 'jpg': $this->type='jpeg'; $this->srcimage=imagecreatefromjpeg($image); break;
	    case 'gif': $this->type=$ext[1]; $this->srcimage=imagecreatefromgif($image); break;
	    default: $this->error=ERROR_UNKNOWN_EXT;
	}
	if ( defined('DEBUG') ) echo "SetImage end\n";
    }

    function SetWtmrkImage($wtmrkimage,$resize=1) {
	if ( defined('DEBUG') ) echo "SetWtmrkImage start\n";
	if ( !is_file($wtmrkimage) ) { $error=ERROR_FILE_NOT_FOUND; return ; }
	if (!$this->srcimage) { $this->error=ERROR_IMAGE_NOT_SET; return ; }
	$im = imagecreatefrompng($wtmrkimage);
	if (!$im) $this->error=ERROR_LOAD_PNG; else $this->$wtmrkimage=$im;
	$x=imagesx($im);
	$y=imagesy($im);
	if ( $resize != 1 ) {
	    if ( defined('DEBUG') ) echo "Resize ratio is $resize\n";
	    $sx=intval($resize*$x);
	    $sy=intval($resize*$y);
	    $newim=imagecreate($sx,$sy);
	    imagecopyresized($newim,$im,0,0,0,0,$sx,$sy,$x,$y);
	    $this->wtmrkimage=$newim;
	} else {
	    $newX=intval(imagesx($this->srcimage)/3);
	    $newY=intval(($newX/$x)*$y);
	    if ( defined('DEBUG') ) echo $x.' '.$y."\n".$newX.' '.$newY."\n";
	    $newim=imagecreate($newX,$newY);
	    //imagecopyresampled
	    imagecopyresized($newim,$im,0,0,0,0,$newX,$newY,$x,$y);
	    $this->wtmrkimage=$newim;
	}
	unset($im,$newim);
	if ( defined('DEBUG') ) echo "SetWtmrkImage end\n";
	return ;
    }

    function ApplyWatermark($name) {
	if ( defined('DEBUG') ) echo "ApplyWatermark start\n";
	if (!$this->posX ) { $this->error=ERROR_IMAGE_X_NOT_SET; return ; }
	if (!$this->posY ) { $this->error=ERROR_IMAGE_Y_NOT_SET; return ; }
	$wix=imagesx($this->wtmrkimage);
	$wiy=imagesy($this->wtmrkimage);
	$transparent=imagecolorat($this->wtmrkimage,0,0);
	for($y=0; $y < $wiy; $y++) {
	    for($x=0;$x < $wix; $x++) {
		if ( imagecolorat($this->wtmrkimage,$x,$y) == $transparent ) continue;
		imagecopymerge($this->srcimage, $this->wtmrkimage,$this->posX+$x, $this->posY+$y, $x, $y, 1, 1, $this->opacity) ;
		if ( defined('DEBUG') ) echo '.';
	    }
	}
	if ( defined('DEBUG') ) echo "\n";
	switch($this->type ) {
	    case 'png':imagepng($this->srcimage, $name); break;
	    case 'jpeg':imagejpeg($this->srcimage, $name); break;
	    case 'gif':imagegif($this->srcimage, $name); break;
	    default: $this->error=ERROR_UNKNOWN_TYPE; return ; break;
	}
	if ( defined('DEBUG') ) echo "ApplyWatermark end\n";
	return ;
    }

    function SetPosition($x,$y) {
	if ( defined('DEBUG') ) echo "SetPosition start\n";
	if (!$this->srcimage) { $this->error=ERROR_IMAGE_NOT_SET; return ; }
	if (!$this->wtmrkimage) { $this->error=ERROR_WTMRKIMAGE_NOT_SET; return ; }
	//if ( imagesx($this->srcimage) < (imagesx($this->wtmrkimage) + $x ) ) { $this->error=ERROR_SET_X; return ; }
	//if ( imagesy($this->srcimage) < (imagesy($this->wtmrkimage) + $y ) ) { $this->error=ERROR_SET_Y; return ; }
	//$this->posX=$x;
	//$this->posY=$y;
	if ( $x == 'left' ) $this->posX=10; else $this->posX=imagesx($this->srcimage)-imagesx($this->wtmrkimage)-10;
	if ( $y == 'up' ) $this->posY=5; else $this->posY=imagesy($this->srcimage)-imagesy($this->wtmrkimage)-5;
	if ( defined('DEBUG') ) echo "SetPosition end\n";
    }

    function SetOpacity($value) {
	if ( ($value > 100) OR ($value < 0) ) $this->error=ERROR_OPACITY;
	else $this->opacity=$value;
	return ;
    }

    function NoError() {
	return $this->error;
    }

    function __destruct() {
	if ( defined('DEBUG') ) echo "Destructor start\n";
	if ( $this->srcimage ) imagedestroy($this->srcimage);
	if ( $this->wtmrkimage ) imagedestroy($this->wtmrkimage);
	if ( defined('DEBUG') ) echo "Destructor end\n";
    }
}


?>